<?php
/**
 * 友情链接控制器
 * Enter description here ...
 * @author Code.404
 *
 */
class FriendlinkAction extends BaseAction {
	/**
	 * 链接列表
	 * Enter description here ...
	 */
	function linkList(){
		$this->assign('linklist' , D('Friendlink')->order('link_style desc , link_priority desc , id asc')->select());
		$this->display();
	}
	/**
	 * 新增链接
	 * Enter description here ...
	 */
	function addLink(){
		if($this->_a == 'show' && $this->_t == 'dialog'){
			$this->display('Friendlink:_addLink');
		}
		if($this->_a == 'exec'){
			$linkModel = D('Friendlink');
			$data = $_POST;
			try{
				$data['link_image'] = $this->imageUpload();
			}catch (Exception $e){
				//$this->ajaxJsonReturn(300,$e->getMessage());
				$data['link_image'] = '';
			}
			try{
				if ($linkModel->addLink($data)){
					$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'), 0 , '' ,'closeCurrent');
				}
				$this->ajaxJsonReturn(300 ,L( $linkModel->getError()));
			}catch(Exception $e){
				$this->ajaxJsonReturn(300,L($e->getMessage()));
			}
		}
	}
	/**
	 * 编辑链接
	 * Enter description here ...
	 */
	function editLink(){
		if($this->_a == 'show' && $this->_t == 'dialog'){
			$this->assign('link',D('Friendlink')->find(FAC::F($_GET['id'],FAC::INT)));
			$this->display('Friendlink:_editFrLink');
		}
		if($this->_a == 'exec'){
			$linkModel = D('Friendlink');
			$data = $_POST;
			$map['id'] = FAC::F($_POST['id'] , FAC::INT);
			try{
				$data['link_image'] = $this->imageUpload();
			}catch (Exception $e){
				//$this->ajaxJsonReturn(300,$e->getMessage());
				$data['link_image'] = $linkModel->where($map)->getField('link_image');
			}
			try{
				if ($linkModel->modifyLink($data)){
					$this->ajaxJsonReturn(200 , L('ACTION_SUCCESS'), 0 , '' ,'closeCurrent');
				}
				$this->ajaxJsonReturn(300 ,L( $linkModel->getError()));
			}catch(Exception $e){
				$this->ajaxJsonReturn(300,L($e->getMessage()));
			}
		}
	}
	/**
	 * 删除链接
	 * Enter description here ...
	 */
	function deleteLink(){
		$this->delete('Friendlink', $_GET['id'] ,U('Friendlink/linkList'));
	}
}